﻿using JuCheap.Core.Infrastructure.Attributes;
using JuCheap.Core.Infrastructure.Extentions;
using JuCheap.Core.Infrastructure.Menu;
using JuCheap.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace JuCheap.Core.Web
{
    public class MenuHelper
    {
        /// <summary>
        /// 获取所有的菜单配置
        /// </summary>
        /// <returns></returns>
        public static List<MenuDto> GetMenues()
        {
            var menus = new List<MenuDto>
            {
                //new MenuDto{Id = Menu.System.Id,Name = Menu.System.Name,Icon = "fa fa-gear"},
                new MenuDto{Id = "00001",Name = "后台管理",Icon = "fa fa-gear"},
                new MenuDto{Id = "00002",Name = "班次设置",Icon = "fa fa-bars"},
                new MenuDto{Id = "00003",Name ="显示管理",Icon = "fa fa-file-o"},
                new MenuDto{Id = "00004",Name = "系统管理",Icon = "fa fa-file-o"}
            };
            menus.Add(new MenuDto()
            {
                Id = "00005",
                ParentId = "00004",
                Name = "用户权限",
                Order = 1,
                Url = $"/user/index".ToLower()
            });
            menus.Add(new MenuDto()
            {
                Id = "00006",
                ParentId = "00004",
                Name = "用户授权",
                Order = 1,
                Url = $"role/authen".ToLower()
            });

            menus.Add(new MenuDto()
            {
                Id = "00007",
                ParentId = "00001",
                Name = "公司管理",
                Order = 1,
                Url = $"company/index".ToLower()
            });
            //menus.Add(new MenuDto()
            //{
            //    Id = "00012",
            //    ParentId = "00001",
            //    Name = "公司管理",
            //    Order = 1,
            //    Url = $"Company/index".ToLower()
            //});

            menus.Add(new MenuDto()
            {
                Id = "00008",
                ParentId = "00001",
                Name = "岗位管理",
                Order = 1,
                Url = $"JobPositionType/index".ToLower()
            });
            menus.Add(new MenuDto()
            {
                Id = "00009",
                ParentId = "00001",
                Name = "人员管理",
                Order = 1,
                Url = $"Worker/index".ToLower()
            });
            menus.Add(new MenuDto()
            {
                Id = "00010",
                ParentId = "00001",
                Name = "设备管理",
                Order = 1,
                Url = $"HardWare/index".ToLower()
            });
            menus.Add(new MenuDto()
            {
                Id = "00011",
                ParentId = "00001",
                Name = "群组管理",
                Order = 1,
                Url = $"DepartmentGroup/index".ToLower()
            });

            menus.Add(new MenuDto()
            {
                Id = "00103",
                ParentId = "00002",
                Name = "时间段设置",
                Order = 1,
                Url = $"TimePeriod/index".ToLower()
            });
            menus.Add(new MenuDto()
            {
                Id = "00101",
                ParentId = "00002",
                Name = "班次设置",
                Order = 1,
                Url = $"BanCi/index".ToLower()
            });
            menus.Add(new MenuDto()
            {
                Id = "00102",
                ParentId = "00002",
                Name = "排班设置",
                Order = 1,
                Url = $"PaiBan/index".ToLower()
            });

            menus.Add(new MenuDto()
            {
                Id = "00201",
                ParentId = "00003",
                Name = "统计数据",
                Order = 1,
                Url = $"Statistics/index".ToLower()
            });
            menus.Add(new MenuDto()
            {
                Id = "00202",
                ParentId = "00003",
                Name = "显示推送",
                Order = 1,
                Url = $"ShowDisplay/index".ToLower()
            });


            #region    
            //var menus = new List<MenuDto>
            //{
            //    new MenuDto{Id = Menu.System.Id,Name = Menu.System.Name,Icon = "fa fa-gear"},
            //    new MenuDto{Id = Menu.Logs.Id,Name = Menu.Logs.Name,Icon = "fa fa-bars"},
            //    new MenuDto{Id = Menu.Pages.Id,Name = Menu.Pages.Name,Icon = "fa fa-file-o"}
            //};

            ////获取所有的控制器
            //var controllers =
            //    from type in typeof(Startup).Assembly.GetTypes()
            //    where type.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)
            //    select type;
            ////遍历所有控制器下面定义了MenuAttribute属性的Action
            //foreach (var controller in controllers)
            //{
            //    var controllerName = controller.Name.Replace("Controller", string.Empty);

            //    var members = controller.GetMembers().Where(x => x.IsDefined(typeof(MenuAttribute)));

            //    foreach (var action in members)
            //    {
            //        var attr = action.GetCustomAttributes<MenuAttribute>().FirstOrDefault();
            //        var actionName = action.Name;

            //        var menu = new MenuDto
            //        {
            //            Id = attr.Id,
            //            ParentId = attr.ParentId,
            //            Name = attr.Name,
            //            Order = attr.Order.ToInt(),
            //            Url = $"/{controllerName}/{actionName}".ToLower()
            //        };
            //        if (menus.Any(x => x.Id == menu.Id))
            //        {
            //            throw new Exception($"已经存在相同的Id={menu.Id},Name={menu.Name}");
            //        }
            //        menus.Add(menu);
            //    }
            //}
            #endregion

            return menus;
        }
    }
}
